#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SQL type wrappers audited by Joshua Kugler, 2008-02-20
"""
The tmpo_du_type table holds data tying event type id values to event type
descriptions

    +----------------------+-----------------+--------------------------+
    |     Column Name      |       Type      |       Description        |
    +----------------------+-----------------+--------------------------+
    | du_type_id           | int,pk,identity | Unique record identifier |
    | description          | varchar         | Description of event type|
    +----------------------+-----------------+--------------------------+

Information from this table is referenced by some of the other APIs, through
the du_type_id key.

The functions found in this API are:

 get_all_types
     selects information out of the database (ids, and descriptions), no filter
     
 get_type_data
     selects id and description out of the database, using the du_type_id
     as the filter
     
 add_type
     inserts a new type of event description into the database, reading
     a form for the description data
     
 update_type
     modifies an event description, using the du_type_id as the filter
     
 delete_type
     deletes an event type id and description from the database; this is
     a physical, not a logical delete.
"""
from string import Template
from tack.db import query, sql_string
from tack.lib import string_app, content_headers, status_codes, json_app
from tack.lib.dicttools import query_to_list
import time

select_all_types = """
SELECT
    *
FROM
    tmpo_du_type
"""

select_type_data = """
SELECT
    *
FROM
    tmpo_du_type
WHERE
    du_type_id = $du_type_id
"""

sql_statement_add_type = """
INSERT INTO
    tmpo_du_type(
        description)
VALUES(
   $description)
"""

sql_statement_update_type = """
UPDATE
    tmpo_du_type
SET
    description=$description
WHERE
    du_type_id = $du_type_id
"""

sql_statement_delete_type = """
DELETE FROM 
    tmpo_du_type 
WHERE
    du_type_id = $du_type_id
"""

def get_all_types(environ, start_response):
    sql = select_all_types
    rows = query(environ['connection'], sql)
    output = {}
    labels = ['id','description'] 
    output['types'] = query_to_list(rows, labels)
    return json_app(output)(environ, start_response)

def get_type_data(environ, start_response):
    du_type_id = environ['dispatcher.groups']['du_type_id']
    sql = Template(select_type_data).substitute(locals())
    rows = query(environ['connection'], sql)
    output = {}
    labels = ['id','description'] 
    output['types'] = query_to_list(rows, labels)
    return json_app(output)(environ, start_response)

def add_type(environ, start_response):
    description = sql_string(environ['form']['description'])
    sql = Template(insert_type).substitute(locals())
    query(environ['connection'], sql)
    rows = query(environ['connection'], 'SELECT @@IDENTITY')
    return json_app(rows)(environ, start_response)

def update_type(environ, start_response):
    description = sql_string(environ['form']['description'])
    du_type_id = environ['dispatcher.groups']['du_type_id']
    sql = Template(type_update).substitute(locals())
    rows = query(environ['connection'], sql)
    output = 'Type Updated'
    return string_app(output)(environ, start_response)

def delete_type(environ, start_response):
    du_type_id = environ['dispatcher.groups']['du_type_id']
    sql = Template(type_delete).substitute(locals())
    rows = query(environ['connection'], sql)
    output = 'Type Deleted'
    return string_app(output)(environ, start_response)

